<?php

namespace App\Http\Controllers\Manager;

use App\Events\ManagerMoneyChangeEvent;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Models\Advert;
use App\Models\AdvertPrice;
use App\Models\ApiLog;
use App\Models\ServerAuth;
use App\Models\ServerEngine;
use App\Models\ServerSpellMall;
use DateInterval;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ServerAuthController extends Controller
{
    public $prefix='manager.serverAuth';

    public function index(){
        return view($this->prefix.'.index');
    }

    public function list(){
        $user=Auth::user();
        $list=$user->serverAuths;
        return ['list'=>$list];

    }

    public function add(Request $request){
        $guid=$request->post('guid');
        $info=$request->post('info');
        $server_engine_id=$request->post('server_engine_id');
        $se=ServerEngine::find($server_engine_id);
        if (!$se){
            return 0;
        }

        if (substr($guid,0,1)!='{') $guid='{'.$guid;
        if (substr($guid,-1,1)!='}') $guid=$guid.'}';

        $item=new ServerAuth();
        $item->user_id=Auth::id();
        $item->guid=$guid;
        $item->info=$info;
        $item->endTime = date("Y-m-d H:m:s",strtotime("+3 days"));
        $item->server_engine_id=$server_engine_id;
//        $ad->status=1;
        $item->save();

        return $item;
    }
    public function del(Request $request){
        $id=$request->get('id');
        $item=ServerAuth::find($id);

        if ($item && $item->user_id==Auth::id()){
            $item->delete();
            return 1;
        }else{
            return -1;
        }
    }

    public function addtime(Request $request){
        $id=$request->get('id');
        $item=ServerAuth::find($id);
        if ($item->server_engine_id){
            $se=ServerEngine::find($item->server_engine_id);
            if ($se){
                $price=$se->price;
                $user=Auth::user();
                if ($price>0){
                    if ($user->remain_money<$price){
                        return ['err'=>1,'msg'=>'积分不足'];
                    }
                    $user->remain_money-=$price;
                    $user->save();
                    event(new ManagerMoneyChangeEvent($user->id,0, 0,8,2,$price,0,$user->remain_money,'购买服务端授权'));

                    $se->user->remain_money+=$price;
                    $se->user->save();
                    event(new ManagerMoneyChangeEvent($se->user->id,0, 0,9,1,$price,0,$se->user->remain_money,'售出服务端授权'));
                }

                $baseDate=now(); //当前时间上添加
                if ($item->endTime && $item->endTime > now()) {
                    $baseDate=date_create($item->endTime); //原有时间上添加
                }
                $di=DateInterval::createFromDateString('1 months');
                $newDate=date_add($baseDate,$di);
                $item->endTime=$newDate->format('Y-m-d H:i:s');
                $item->save();



                return ['err'=>0,'data'=>$item];
            }else{
                return ['err'=>1,'msg'=>'无效的服务端'];
            }
        }else{
            return ['err'=>1,'msg'=>'无效的服务端'];
        }

    }

}
